package org.sean.framework.web.filter;

import lombok.Builder;
import org.springframework.core.Ordered;
import org.springframework.web.filter.RequestContextFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 可变请求头
 */
@Builder
public class MutableHeaderFilter extends RequestContextFilter implements Ordered {

    private MutableHeaderCallback callback;

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        MutableHeaderRequestWrapper wrapper = new MutableHeaderRequestWrapper(request);
        if (callback != null) {
            callback.callback(wrapper);
        }
        super.doFilterInternal(wrapper, response, filterChain);
    }

    @Override
    public int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE + 50;
    }
}
